﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataAccess;
using RM2012Service.Business;
using System.Data;

namespace RM2012Service.Controller
{
    public class UserGroupCtrl
    {
        UserGroupDAO dao;

        public UserGroupCtrl()
        {
            dao = new UserGroupDAO();
        }

        public UserGroupInfo[] GetAllGroup()
        {
            UserGroupInfo[] list = null;
            try
            {
                DataTable dt = dao.GetAllGroup();
                if (dt.Rows.Count > 0)
                {
                    list = new UserGroupInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UserGroupInfo(dt.Rows[i]["UserGroupId"].ToString(),
                            dt.Rows[i]["UserGroupName"].ToString(),
                            (bool)dt.Rows[i]["IsDefault"],
                            (bool)dt.Rows[i]["IsAdmin"],
                            (bool)dt.Rows[i]["IsMod"],
                            (bool)dt.Rows[i]["CanDelete"],
                            (bool)dt.Rows[i]["CanReport"],
                            (bool)dt.Rows[i]["CanEdit"],
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public UserGroupInfo GetGroupById(string groupId)
        {
            UserGroupInfo info = null;
            try
            {
                DataTable dt = dao.GetGroupById(groupId);
                if (dt.Rows.Count > 0)
                {
                    info = new UserGroupInfo(dt.Rows[0]["UserGroupId"].ToString(),
                            dt.Rows[0]["UserGroupName"].ToString(),
                            (bool)dt.Rows[0]["IsDefault"],
                            (bool)dt.Rows[0]["IsAdmin"],
                            (bool)dt.Rows[0]["IsMod"],
                            (bool)dt.Rows[0]["CanDelete"],
                            (bool)dt.Rows[0]["CanReport"],
                            (bool)dt.Rows[0]["CanEdit"],
                            dt.Rows[0]["Remark"].ToString());
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }

        public UserGroupInfo[] GetGroupByLikeId(string groupId)
        {
            UserGroupInfo[] list = null;
            try
            {
                DataTable dt = dao.GetGroupByLikeId(groupId);
                if (dt.Rows.Count > 0)
                {
                    list = new UserGroupInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UserGroupInfo(dt.Rows[i]["UserGroupId"].ToString(),
                            dt.Rows[i]["UserGroupName"].ToString(),
                            (bool)dt.Rows[i]["IsDefault"],
                            (bool)dt.Rows[i]["IsAdmin"],
                            (bool)dt.Rows[i]["IsMod"],
                            (bool)dt.Rows[i]["CanDelete"],
                            (bool)dt.Rows[i]["CanReport"],
                            (bool)dt.Rows[i]["CanEdit"],
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public UserGroupInfo[] GetGroupByName(string groupName)
        {
            UserGroupInfo[] list = null;
            try
            {
                DataTable dt = dao.GetGroupByName(groupName);
                if (dt.Rows.Count > 0)
                {
                    list = new UserGroupInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UserGroupInfo(dt.Rows[i]["UserGroupId"].ToString(),
                            dt.Rows[i]["UserGroupName"].ToString(),
                            (bool)dt.Rows[i]["IsDefault"],
                            (bool)dt.Rows[i]["IsAdmin"],
                            (bool)dt.Rows[i]["IsMod"],
                            (bool)dt.Rows[i]["CanDelete"],
                            (bool)dt.Rows[i]["CanReport"],
                            (bool)dt.Rows[i]["CanEdit"],
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public UserGroupInfo GetGroupByUserId(string userId)
        {
            UserGroupInfo info = null;
            try
            {
                DataTable dt = dao.GetGroupByUserId(userId);
                if (dt.Rows.Count > 0)
                {
                    info = new UserGroupInfo(dt.Rows[0]["UserGroupId"].ToString(),
                            dt.Rows[0]["UserGroupName"].ToString(),
                            (bool)dt.Rows[0]["IsDefault"],
                            (bool)dt.Rows[0]["IsAdmin"],
                            (bool)dt.Rows[0]["IsMod"],
                            (bool)dt.Rows[0]["CanDelete"],
                            (bool)dt.Rows[0]["CanReport"],
                            (bool)dt.Rows[0]["CanEdit"],
                            dt.Rows[0]["Remark"].ToString());
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }

        public bool AddGroup(UserGroupInfo info)
        {
            if (dao.AddGroup(info) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateGroup(UserGroupInfo info)
        {
            if (dao.UpdateGroup(info) > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool DeleteGroup(string groupId)
        {
            if (dao.DeleteGroup(groupId) > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }
    }
}
